package CPANStuff;

use strict;
use warnings;

use CPAN;
use CPAN::Config;
use List::Util;

sub new {
    my $class = shift;
    my $self  = {};
    bless $self, $class;
    return $self;
}

sub install_wrapper {
    my $self = shift or die "no object";
    my $mod = shift or die "no mod";

    my $cpan = CPAN->new();
    return if $cpan->has_inst($mod);

    $self->prereq_policy("follow");
    if ( my $result = CPAN::install ($mod) ) {
      die "couldn't install: $mod, result: $result";
    }
    $self->prereq_policy("ask");
}

sub force_install_wrapper {
    my $self = shift or die "no object";
    my $mod = shift or die "no mod";

    my $cpan = CPAN->new();
    return if $cpan->has_inst($mod);

    $self->prereq_policy("follow");
    if ( my $result = CPAN::force('install', $mod) ) {
      die "couldn't force install: $mod, result: $result";
    }
    $self->prereq_policy("ask");
}

sub prereq_policy {
    my $self = shift or die "no object";
    my $policy = shift or die "no policy";
    CPAN::Shell->o("conf", "defaults");
    CPAN::Shell->o("conf", "prerequisites_policy", $policy);
    CPAN::Shell->o("conf", "commit");
}

1;
